package com.qiaoba.module.generator.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.qiaoba.common.base.entity.Result;
import com.qiaoba.common.database.entity.PageQuery;
import com.qiaoba.common.database.entity.PageResult;
import com.qiaoba.module.generator.constant.GenConstants;
import com.qiaoba.module.generator.entity.GeneratorTable;
import com.qiaoba.module.generator.entity.GeneratorTableColumn;
import com.qiaoba.module.generator.entity.dto.GeneratorTableDto;
import com.qiaoba.module.generator.entity.dto.TableDto;
import com.qiaoba.module.generator.service.GeneratorTableColumnService;
import com.qiaoba.module.generator.service.GeneratorTableService;
import com.qiaoba.module.generator.util.GeneratorUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代码生成 Web层
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/6/26 9:09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/tool/gen")
public class GeneratorController {

    private final GeneratorTableService generatorTableService;
    private final GeneratorTableColumnService generatorTableColumnService;

    /**
     * 代码生成列表
     */
    @GetMapping("/list")
    public PageResult list(TableDto dto, PageQuery pageQuery) {
        return generatorTableService.selectPageList(dto, pageQuery);
    }

    /**
     * 代码生成详细
     */
    @GetMapping(value = "/{tableId}")
    public Result getInfo(@PathVariable String tableId) {
        GeneratorTable table = generatorTableService.selectById(tableId);
        List<GeneratorTableColumn> list = generatorTableColumnService.selectListByTableId(tableId, false);
        Map<String, Object> map = new HashMap<String, Object>(2);
        map.put("info", table);
        map.put("rows", list);
        return Result.success(map);
    }

    /**
     * 修改代码生成
     */
    @PutMapping
    public Result edit(@RequestBody GeneratorTableDto dto) {
        GeneratorTable generatorTable = BeanUtil.copyProperties(dto, GeneratorTable.class);
        int result = generatorTableService.updateById(generatorTable);
        generatorTableColumnService.updateBatchById(dto.getColumns());
        return Result.toAjax(result);
    }

    /**
     * 批量删除代码生成
     */
    @DeleteMapping("/{tableIds}")
    public Result remove(@PathVariable List<String> tableIds) {
        return Result.toAjax(generatorTableService.deleteByIds(tableIds));
    }

    /**
     * 数据库表列表(排除已导入表)
     */
    @GetMapping("/db/list")
    public PageResult dbTableList(TableDto dto, PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize());
        return PageResult.build(generatorTableService.selectDbTableList(dto));
    }

    /**
     * 导入表结构
     */
    @PostMapping("/importTable")
    public Result importTable(String tables) {
        generatorTableService.importTable(tables);
        return Result.success();
    }

    /**
     * 预览代码
     */
    @GetMapping("/preview/{tableId}")
    public Result preview(@PathVariable("tableId") String tableId) {
        Map<String, String> dataMap = generatorTableService.previewCode(tableId, GenConstants.TEMPLATE_TYPE_ALL);
        return Result.success(dataMap);
    }

    /**
     * 下载文件
     */
    @GetMapping("/download/{tableId}/{templateType}")
    public void download(HttpServletResponse response, @PathVariable String tableId, @PathVariable String templateType) throws IOException {
        byte[] data = generatorTableService.downloadCode(tableId, templateType);
        GeneratorUtil.download(response, data);
    }

    /**
     * 同步表结构
     */
    @GetMapping("/syncTable/{tableId}")
    public Result syncTable(@PathVariable String tableId) {
        generatorTableService.syncTable(tableId);
        return Result.success();
    }

}
